package com.shinbada.modules.sys.web;

import com.shinbada.common.json.AjaxJson;
import com.shinbada.common.utils.MyBeanUtils;
import com.shinbada.core.web.BaseController;
import com.shinbada.modules.sys.entity.SysSetting;
import com.shinbada.modules.sys.service.SysSettingService;
import com.shinbada.modules.sys.utils.UserUtils;
import com.shinbada.modules.sys.vo.SysSettingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统配置
 *
 * @author initcap
 * @date 2021/3/11 10:50
 */
@RestController
@RequestMapping("/sys/sysSetting")
@Api(tags = "系统配置")
public class SysSettingController extends BaseController {

    @Autowired
    private SysSettingService sysSettingService;

    @RequiresPermissions("user")
    @ApiOperation("系统配置信息")
    @GetMapping("conf")
    public AjaxJson queryById() {
        return AjaxJson.success().put("setting", UserUtils.getSetting());
    }

    @RequiresPermissions("user")
    @GetMapping("pages")
    @ApiOperation("获取系统配置,用户页面样式初始化")
    public AjaxJson getSetting() {
        SysSetting setting = UserUtils.getSetting();
        SysSettingVo vo = new SysSettingVo();
        vo.setDefaultLayout(setting.getDefaultLayout());
        vo.setDefaultTheme(setting.getDefaultTheme());
        vo.setLogo(setting.getLogo());
        vo.setLogoLong(setting.getLogoLong());
        vo.setDefaultThemeStyle(setting.getDefaultThemeStyle());
        vo.setOpenTab(setting.getOpenTab());
        return AjaxJson.success().put("setting", vo);
    }

    @RequiresPermissions("user")
    @PostMapping("save")
    @ApiOperation("保存系统配置")
    public AjaxJson save(SysSetting setting) throws Exception {
        SysSetting target = UserUtils.getSetting();
        MyBeanUtils.copyBeanNotNull2Bean(setting, target);
        target.setIsNewRecord(false);
        sysSettingService.save(target);
        return AjaxJson.success("保存系统配置成功").put("setting", target);
    }

}
